﻿using System;
using System.Collections.Generic;
using Jayden.Dll.Web.Enums;
using Jayden.Dll.Web.HtmlControls.Descriptions;
using Jayden.Dll.Web.HtmlControls.Generics;
using Jayden.Dll.Web.HtmlControls.Interfaces;

namespace Jayden.Dll.Web.HtmlControls
{
	public class Option : HtmlElement, WithDisabled, WithLabel, WithValue, WithSelected
	{
		public Option() : base(OptionDescription.Instance) { }
		public Option(string value, string text)
			: this()
		{
			if(value != null)
				this["value"] = value;
			if (text != null)
				AddText(text);
		}

		public string Value { get { return this["value"]; } set { this["value"] = value; } }
		public string Label { get { return this["label"]; } set { this["label"] = value; } }
		public bool Disabled { get { return GetAttributeBool("disabled"); } set { SetAttribute("disabled", value); } }
		public bool Enabled { get { return !Disabled; } set { Disabled = !value; } }
		public bool Selected { get { return GetAttributeBool("selected"); } set { SetAttribute("selected", value); } }

		protected override void TranslateAttributes(Core.Translations.ITranslator translator)
		{
			base.TranslateAttributes(translator);
			Label = translator.Translate(Label);
		}

	}
	public class OptionDescription : HtmlElementDescription
	{
		public static readonly OptionDescription Instance = new OptionDescription();
		private OptionDescription() : base("option", HtmlVersion.Html4, true, true) { }
		public override Type ControlType { get { return typeof(Option); } }
		public override string Description { get { return "option in a select control, or an option in a labelled set of options grouped together in an optgroup, or an option among the list of suggestions in a datalist."; } }
		public override IEnumerable<HtmlElementDescription> PermittedElements { get { yield break; } }
		public override IEnumerable<HtmlElementDescription> PermittedParents
		{
			get
			{
				yield return OptGroupDescription.Instance;
				yield return SelectDescription.Instance;
				yield return DataListDescription.Instance;
			}
		}
	}
}
